classdef Preparser < exception.Exception
    enumeration
        INTERNAL ( ...
            'error', ...
            'Internal IRIS preparser error. Please report this error to IRIS Solutions Team.' ...
            );
        ERROR_PREPARSING ( ...
            'error', ...
            '%s' ...
            )
        CANNOT_FIND_FILE ( ...
            'error', ...
            'Cannot find this file: %s ' ...
            );
        CANNOT_OPEN_FILE ( ...
            'error', ...
            'Cannot open this file for reading: %s ' ...
            );
        PSEUDOSUBS_NESTED ( ...
            'error', ...
            'This pseudosubstitution is nested within another: %s ' ...
            )
        PSEUDOSUBS_NOT_CLOSED ( ...
            'error', ...
            'This pseudosubstitution is not properly closed: %s ' ...
            )
        PSEUDOSUBS_EVAL_FAILED ( ...
            'error', ...
            'Error evaluating this pseudosubstitution: %s ' ...
            )
        CTRL_MISSING_END ( ...
            'error', ...
            'No matching !end found for this control command: %s ' ...
            )
        CTRL_UNFINISHED_CONDITION ( ...
            'error', ...
            'This control condition or expression is unfinished: %s ' ...
            )
        CTRL_LEFTOVER ( ...
            'error', ...
            'This leftover code found in !switch block: %s ' ...
            )
        CTRL_FOR_MULTIPLE_DO ( ...
            'error', ...
            'This !for block contains multiple !do keywords: %s ' ...
            )
        CTRL_OBSOLETE_UPPER_LOWER ( ...
            'error', ...
            'This is obsolete syntax for control variables in !for blocks: %s ' ...
            )
        SUBS_LEFTOVER ( ...
            'error', ...
            'This leftover code found in !substitution block: %s ' ...
            )
        SUBS_NAME_MULTIPLE ( ...
            'error', ...
            'This substitution name is defined more than once: %s ' ...
            );
        SUBS_UNDEFINED ( ...
            'error', ...
            'This substitution is not defined: %s ' ...
            );
        PSEUDOFUNC_UNFINISHED ( ...
            'error', ...
            'No matching closing braket found for this pseudofunction: %s ' ...
            );
        PSEUDOFUNC_SECOND_INPUT_FAILED ( ...
            'error', ...
            ['Second input argument to this pseudofunction failed ', ...
            'to evaluate to a scalar number: %s '] ...
            );
        CANNOT_SAVE ( ...
            'error', ...
            'Cannot save preparsed code to disk file: %s ' ...
            );
        CLONE_STRING_INVALID ( ...
            'error', ...
            'This is not a valid clone string: %s ' ...
            )
    end
    
    
    
    
    enumeration
        CANNOT_CLOSE_FILE ( ...
            'warning', ...
            'Cannot close this file after reading: %s ' ...
            );
        CTRL_EVAL_IF_FAILED ( ...
            'warning', ...
            ['Error evaluating this !if or !elseif condition; ', ...
            'assuming false instead: %s '] ...
            );
        CTRL_EVAL_SWITCH_FAILED ( ...
            'warning', ...
            ['Error evaluating this !switch expression; ', ...
            'proceeding to !otherwise or !end: %s '] ...
            );
        CTRL_EVAL_CASE_FAILED ( ...
            'warning', ...
            ['Error evaluating this !case expression; ', ...
            'proceeding to next !case, !otherwise or !end: %s '] ...
            );
    end
    
    
    
    
    properties (Constant)
        CLASS_IDENTIFIER = 'Preparser';
        PREPARSER_HEADER_FORMAT = ' when preparsing %s ';
        PREPARSER_HEADER_EMPTY_FORMAT = ' when preparsing input string';
    end
    
    
    
    
    methods
        function header = createHeader(this)
            header = createHeader@exception.Exception(this);
            fileName = exception.Preparser.storeFileName( );
            if ~isempty(fileName)
                addHeader = sprintf( ...
                    exception.Preparser.PREPARSER_HEADER_FORMAT, ...
                    fileName ...
                    );
            else
                addHeader = exception.Preparser.PREPARSER_HEADER_EMPTY_FORMAT;
            end
            header = [ header, addHeader ];
        end
    end
    
    
    
    
    methods (Static)
        function varargout = storeFileName(varargin)
            persistent FILE_NAME;
            if nargout==0
                if nargin==0
                    FILE_NAME = '';
                else
                    FILE_NAME = varargin{1};
                end
            else
                if isempty(FILE_NAME) || ~ischar(FILE_NAME)
                    FILE_NAME = '';
                end
                varargout{1} = FILE_NAME;
            end
        end
    end
end